﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class discount_manage : System.Web.UI.Page
{
    protected DataTable result = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (!IsPostBack)
        {
            result.Columns.Add("color");
            result.Columns.Add("d_title");
            result.Columns.Add("d_id");
            result.Columns.Add("d_image");
            result.Columns.Add("d_rate");
            result.Columns.Add("d_type");
            result.Columns.Add("d_start");
            result.Columns.Add("d_end");
            result.Columns.Add("d_vip");
            string name = Session["USERNAME"].ToString();
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();

            SqlCommand get_discount = new SqlCommand();
            get_discount.Connection = sc1;
            get_discount.CommandText = "select DISCOUNT_ID,DISCOUNT_TITLE,DISCOUNT_IMAGE,VIP,DISCOUNT_START,DISCOUNT_END,DISCOUNT_TYPE,DIS_RATE from INFO_DISCOUNT where ENTER_ID=@eid1";
           
            get_discount.Parameters.AddWithValue("@eid1",Session["ENTERID"].ToString());
            SqlDataReader r = get_discount.ExecuteReader();
            DateTime start  ;
            DateTime end ;
            DateTime now = DateTime.Now;
            while (r.Read())
            {
                DataRow row = result.NewRow();
                row["d_title"] = r.GetString(r.GetOrdinal("DISCOUNT_TITLE"));
                row["d_id"] = r.GetString(r.GetOrdinal("DISCOUNT_ID"));
                row["d_image"] = r.GetString(r.GetOrdinal("DISCOUNT_IMAGE"));
                if (r.GetInt32(r.GetOrdinal("VIP")) == 1)
                    row["d_vip"] = "true";
                else
                    row["d_vip"] = "false";
                row["d_rate"] = r.GetString(r.GetOrdinal("DIS_RATE"));
                row["d_type"] = r.GetString(r.GetOrdinal("DISCOUNT_TYPE"));
                start = r.GetDateTime(r.GetOrdinal("DISCOUNT_START"));
                end = r.GetDateTime(r.GetOrdinal("DISCOUNT_END"));
                row["d_start"] = start.ToString();
                row["d_end"] = end.ToString();
                if (now > start && now < end)
                {
                    row["color"] = "Red";
                }
                else row["color"] = "Black";
                result.Rows.Add(row);
            }
            DataBind();
            sc1.Close();
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (Session["IDENTITY"].Equals("employee") == true)
        {           
                Response.Redirect("error_auth.aspx", true);
        }
        if (e.CommandName == "undercarriage")
        {
            string id = e.CommandArgument.ToString();
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            SqlCommand del_pro = new SqlCommand("delete from DISCOUNT_PRODUCT where DISCOUNT_ID=@idd",sc1);
            del_pro.Parameters.AddWithValue("@idd",id);
            while (Application["sqllock"] == "lock")
            {//检测是否已被锁住
                System.Threading.Thread.Sleep(2000);
            }
            Application["sqllock"] = "lock";//上锁          
            try
            {
                del_pro.ExecuteNonQuery();
            }
            catch (Exception ex_sql)
            {
                Application["sqllock"] = "unlock";//解锁
                Page.RegisterStartupScript("ss", "<script>alert('" + ex_sql.StackTrace + ex_sql.Message + "')</script>");
                return;
            }
            finally
            {
                Application["sqllock"] = "unlock";//解锁
            }

            SqlCommand get_image = new SqlCommand("select DISCOUNT_IMAGE from INFO_DISCOUNT where DISCOUNT_ID=@iddd",sc1);
            get_image.Parameters.AddWithValue("@iddd",id);
            string imagepath = get_image.ExecuteScalar().ToString();
            imagepath = Server.MapPath(imagepath);
            SqlCommand get_name = new SqlCommand("select DISCOUNT_TITLE from INFO_DISCOUNT where DISCOUNT_ID=@id2",sc1);
            get_name.Parameters.AddWithValue("@id2",id);
            string d_name = get_name.ExecuteScalar().ToString();

                if (File.Exists(imagepath))
                    File.Delete(imagepath);
                //删除DISCOUNT_ATTEND中的记录并记录到ATTEND_RECORD当中
                label3.Text="开始迁移您的数据至备份数据库中...";
                #region 迁移参与记录到历史记录表中
                string[] indid = new string[1000];//预留1000条记录的容量
                DateTime[] adate = new DateTime[1000];
                SqlCommand get_indid = new SqlCommand("select IND_ID,ATTEND_DATE from DISCOUNT_ATTEND where DISCOUNT_ID=@id3",sc1);
                get_indid.Parameters.AddWithValue("@id3",id);
                SqlDataReader s = get_indid.ExecuteReader();
                int tt = 0;
                while (s.Read())
                {
                    indid[tt] = s.GetString(s.GetOrdinal("IND_ID"));
                    adate[tt] = s.GetDateTime(s.GetOrdinal("ATTEND_DATE"));
                    tt++;
                }
                s.Close();
                //将获取的记录一一插入ATTENDRECORD
                for(int p=0;p<tt;p++)
                {
                SqlCommand insert_record = new SqlCommand("insert into ATTEND_RECORD values (@id4,@etype,@ename,@adate)",sc1);
                insert_record.Parameters.AddWithValue("@id4",indid[p]);
                insert_record.Parameters.AddWithValue("@etype","DISCOUNT");
                insert_record.Parameters.AddWithValue("@ename",d_name);
                insert_record.Parameters.AddWithValue("@adate",adate[p]);
                while (Application["sqllock"] == "lock")
                {//检测是否已被锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁          
                try
                {
                  insert_record.ExecuteNonQuery();
                }
                catch (Exception ex_sql)
                {
                    Application["sqllock"] = "unlock";//解锁
                    Page.RegisterStartupScript("ss", "<script>alert('" + ex_sql.StackTrace + ex_sql.Message + "')</script>");
                    return;
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁
                }
                }
                //将DISCOUNT_ATTEND中记录删除
                SqlCommand del_at=new SqlCommand("delete from DISCOUNT_ATTEND where DISCOUNT_ID=@id5",sc1);
                del_at.Parameters.AddWithValue("@id5",id);
                 while (Application["sqllock"] == "lock")
                {//检测是否已被锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁          
                try
                {
                  del_at.ExecuteNonQuery();
                }
                 finally
                {
                    Application["sqllock"] = "unlock";//解锁
                }

                
                #endregion
                label3.Text = "数据迁移完毕.";
                SqlCommand del_dis = new SqlCommand("delete from INFO_DISCOUNT where DISCOUNT_ID=@id", sc1);
                del_dis.Parameters.AddWithValue("@id", id);
                while (Application["sqllock"] == "lock")
                {//检测是否已被锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁
                try
                {
                    del_dis.ExecuteNonQuery();
                }
                finally
                {

                    Application["sqllock"] = "unlock";//解锁
                }
                Page.RegisterStartupScript("ss", "<script>alert('下架成功！')</script>");
                return;
            
           

        }
        else if (e.CommandName == "oncarriage")
        {
            string id = e.CommandArgument.ToString();
            Response.Redirect("discount_product.aspx?did="+id);
        }
    }
}